package logic

import (
	"context"
	"errors"
	"good/models/mysql"
	"strings"

	"good/goodrpc/goodrpc"
	"good/goodrpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type SpeakAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewSpeakAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SpeakAddLogic {
	return &SpeakAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *SpeakAddLogic) SpeakAdd(in *goodrpc.SpeakAddRequest) (*goodrpc.SpeakAddResponse, error) {
	// todo: add your logic here and delete this line
	speak := mysql.Speak{
		UserId: int(in.UserId),
		Text:   in.Text,
		GoodId: int(in.GoodId),
	}
	if strings.Contains(in.Text, "欺诈") || strings.Contains(in.Text, "伪劣") {
		return nil, errors.New("评论违规！")
	}

	mysql.DB.Debug().Create(&speak)

	return &goodrpc.SpeakAddResponse{
		SpeakId: int64(speak.ID),
	}, nil
}
